/**
 * Created with JetBrains WebStorm.
 * User: Joseph
 * Date: 29/04/12
 * Time: 1:33 AM
 * To change this template use File | Settings | File Templates.
 */




function MakePiece(width, height, x, y, url) {
    this.width = width + 'px';
    this.height = height + 'px';
    this.x = x + 'px';
    this.y = y + 'px';
    this.img = url;
}


function act_move(key) {
    temp = JSON.parse(state_[key]);
    temp.x = $('#' + key).css('left');
    temp.y = $('#' + key).css('top');

    var submit = {};
    var change = {};

    for (var i = 0, iLen = participants_.length; i < iLen; ++i) {
        var p = participants_[i];
        var temp_string = gapi.hangout.data.getValue('render' + p.id);

        if (temp_string != undefined) {
            var x = (1 + parseInt(temp_string));
            submit['render' + p.id] = x.toString();
        }
    }

    temp_string = gapi.hangout.data.getValue('_event_order' + key);
    if (temp_string != undefined) {
        change['event_order'] = temp_string;
    } else {
        change['event_order'] = '0';
        gapi.hangout.data.setValue('_event_order' + key, '0');
    }

    change['piece'] = key;
    change['item'] = 'move';
    change['x'] = temp.x;
    change['y'] = temp.y;


    submit['_temp' + key] = JSON.stringify(temp);
    submit['_change' + key + Math.random()] = JSON.stringify(change);
    saveValue(submit)
}


function act_rolldice(key) {
    temp = JSON.parse(state_[key]);
    var result = Math.ceil(Math.random() * 6);
    temp.img = images['dice' + result];

    var submit = {};
    var change = {};

    for (var i = 0, iLen = participants_.length; i < iLen; ++i) {
        var p = participants_[i];
        var temp_string = gapi.hangout.data.getValue('render' + p.id);
        if (temp_string != undefined) {
            var x = (1 + parseInt(temp_string));
            submit['render' + p.id] = x.toString();
        }
    }

    temp_string = gapi.hangout.data.getValue('_event_order' + key);
    if (temp_string != undefined) {
        change['event_order'] = temp_string;
    } else {
        change['event_order'] = '0';
        gapi.hangout.data.setValue('_event_order' + key, '0');
    }

    change['piece'] = key;
    change['item'] = 'rolldice';
    change['result'] = result;

    submit['_temp' + key] = JSON.stringify(temp);
    submit['_change' + key + Math.random()] = JSON.stringify(change);
    console.log('rolldice');
    saveValue(submit)
}


function act_flipcoin(key) {
    temp = JSON.parse(state_[key]);
    var result = Math.random() * 2;

    if (result > 1) {
        temp.img = images['coin_head'];
    } else {
        temp.img = images['coin_tail'];
    }

    var submit = {};
    var change = {};

    for (var i = 0, iLen = participants_.length; i < iLen; ++i) {
        var p = participants_[i];
        var temp_string = gapi.hangout.data.getValue('render' + p.id);
        if (temp_string != undefined) {
            var x = (1 + parseInt(temp_string));
            submit['render' + p.id] = x.toString();
        }
    }

    temp_string = gapi.hangout.data.getValue('_event_order' + key);
    if (temp_string != undefined) {
        change['event_order'] = temp_string;
    } else {
        change['event_order'] = '0';
        gapi.hangout.data.setValue('_event_order' + key, '0');
    }

    change['piece'] = key;
    change['item'] = 'flipcoin';
    change['result'] = result;

    submit['_temp' + key] = JSON.stringify(temp);
    submit['_change' + key + Math.random()] = JSON.stringify(change);
    console.log('rolldice');
    saveValue(submit)
}
